<!DOCTYPE html>
<html lang="zh-CN">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://img2.jimu98.cn/blog/20200729222712.png">
  <link rel="icon" type="image/png" href="https://img2.jimu98.cn/blog/20200729222712.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="你会的越多,你不会的越多,我是积木,互联网的一只小菜鸡">
  <meta name="author" content="积木">
  <meta name="keywords" content="积木,jimu98,java,后端,jimu">
  <title>【项目创建流程】MyBatisPlas流程 - 积木 | 互联网的一只小菜鸡</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script data-ad-client="ca-pub-2806771379080352" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<meta name="generator" content="Hexo 4.2.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>jimu98</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/list/">
                <i class="iconfont icon-bookmark-fill"></i>
                导航
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('https://img2.jimu98.cn/blog/20200729222405.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-07-08 16:08">
      2020年7月8日 下午
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.8k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      25
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h1 id="【项目创建流程】MyBatisPlas流程"><a href="#【项目创建流程】MyBatisPlas流程" class="headerlink" title="【项目创建流程】MyBatisPlas流程"></a>【项目创建流程】MyBatisPlas流程</h1><h2 id="MP简介"><a href="#MP简介" class="headerlink" title="MP简介"></a>MP简介</h2><p><a href="https://github.com/baomidou/mybatis-plus" target="_blank" rel="noopener">MyBatis-Plus</a>（简称 MP）是一个 <a href="http://www.mybatis.org/mybatis-3/" target="_blank" rel="noopener">MyBatis</a> 的增强工具，在 MyBatis 的基础上只做增强不做改变，为简化开发、提高效率而生。</p>
<ul>
<li><p>润物无声  </p>
</li>
<li><ul>
<li>只做增强不做改变，引入它不会对现有工程产生影响，如丝般顺滑。</li>
</ul>
</li>
<li><p>效率至上</p>
</li>
<li><ul>
<li>只需简单配置，即可快速进行 CRUD 操作，从而节省大量时间。</li>
</ul>
</li>
<li><p>丰富功能</p>
</li>
<li><ul>
<li>热加载、代码生成、分页、性能分析等功能一应俱全。</li>
</ul>
</li>
</ul>
<h2 id="使用流程"><a href="#使用流程" class="headerlink" title="使用流程"></a>使用流程</h2><h3 id="创建代码生成器"><a href="#创建代码生成器" class="headerlink" title="创建代码生成器"></a>创建代码生成器</h3><pre><code class="hljs java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CodeGenerator</span> </span>&#123;

    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">genCode</span><span class="hljs-params">()</span> </span>&#123;

        <span class="hljs-comment">//String prefix = "dbxxx_";</span>
        String moduleName = <span class="hljs-string">"statistics"</span>;

        <span class="hljs-comment">// 1、创建代码生成器</span>
        AutoGenerator mpg = <span class="hljs-keyword">new</span> AutoGenerator();

        <span class="hljs-comment">// 2、全局配置</span>
        GlobalConfig gc = <span class="hljs-keyword">new</span> GlobalConfig();
        String projectPath = System.getProperty(<span class="hljs-string">"user.dir"</span>);
        gc.setOutputDir(projectPath + <span class="hljs-string">"/src/main/java"</span>);
        gc.setAuthor(<span class="hljs-string">"jimu98"</span>);
        gc.setOpen(<span class="hljs-keyword">false</span>); <span class="hljs-comment">//生成后是否打开资源管理器</span>
<span class="hljs-comment">//        gc.setFileOverride(false); //重新生成时文件是否覆盖</span>
        gc.setServiceName(<span class="hljs-string">"%sService"</span>);	<span class="hljs-comment">//去掉Service接口的首字母I</span>
        gc.setIdType(IdType.ASSIGN_ID); <span class="hljs-comment">//主键策略</span>
        gc.setDateType(DateType.ONLY_DATE);<span class="hljs-comment">//定义生成的实体类中日期类型</span>
        gc.setSwagger2(<span class="hljs-keyword">true</span>);<span class="hljs-comment">//开启Swagger2模式</span>
        mpg.setGlobalConfig(gc);

        <span class="hljs-comment">// 3、数据源配置</span>
        DataSourceConfig dsc = <span class="hljs-keyword">new</span> DataSourceConfig();
        dsc.setUrl(<span class="hljs-string">"jdbc:mysql://localhost:3306/"</span>  + <span class="hljs-string">"guli_"</span> + moduleName + <span class="hljs-string">"?serverTimezone=GMT%2B8"</span>);
        dsc.setDriverName(<span class="hljs-string">"com.mysql.cj.jdbc.Driver"</span>);
        dsc.setUsername(<span class="hljs-string">"root"</span>);
        dsc.setPassword(<span class="hljs-string">""</span>);
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        <span class="hljs-comment">// 4、包配置</span>
        PackageConfig pc = <span class="hljs-keyword">new</span> PackageConfig();
        pc.setModuleName(moduleName); <span class="hljs-comment">//模块名</span>
        pc.setParent(<span class="hljs-string">"cn.jimu98.guli.service"</span>);
        pc.setController(<span class="hljs-string">"controller"</span>);
        pc.setEntity(<span class="hljs-string">"entity"</span>);
        pc.setService(<span class="hljs-string">"service"</span>);
        pc.setMapper(<span class="hljs-string">"mapper"</span>);
        mpg.setPackageInfo(pc);

        <span class="hljs-comment">// 5、策略配置</span>
        StrategyConfig strategy = <span class="hljs-keyword">new</span> StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);<span class="hljs-comment">//数据库表映射到实体的命名策略</span>
        strategy.setTablePrefix(moduleName + <span class="hljs-string">"_"</span>);<span class="hljs-comment">//设置表前缀不生成</span>

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);<span class="hljs-comment">//数据库表字段映射到实体的命名策略</span>
        strategy.setEntityLombokModel(<span class="hljs-keyword">true</span>); <span class="hljs-comment">// lombok 模型 @Accessors(chain = true) setter链式操作</span>

        strategy.setLogicDeleteFieldName(<span class="hljs-string">"is_deleted"</span>);<span class="hljs-comment">//逻辑删除字段名</span>
        strategy.setEntityBooleanColumnRemoveIsPrefix(<span class="hljs-keyword">true</span>);<span class="hljs-comment">//去掉布尔值的is_前缀</span>

        <span class="hljs-comment">//自动填充</span>
        TableFill gmtCreate = <span class="hljs-keyword">new</span> TableFill(<span class="hljs-string">"gmt_create"</span>, FieldFill.INSERT);
        TableFill gmtModified = <span class="hljs-keyword">new</span> TableFill(<span class="hljs-string">"gmt_modified"</span>, FieldFill.INSERT_UPDATE);
        ArrayList&lt;TableFill&gt; tableFills = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);

        strategy.setRestControllerStyle(<span class="hljs-keyword">true</span>); <span class="hljs-comment">//restful api风格控制器</span>
        strategy.setControllerMappingHyphenStyle(<span class="hljs-keyword">true</span>); <span class="hljs-comment">//url中驼峰转连字符</span>
        mpg.setStrategy(strategy);

        <span class="hljs-comment">//设置BaseEntity</span>
        strategy.setSuperEntityClass(<span class="hljs-string">"cn.jimu98.guli.service.base.model.BaseEntity"</span>);
        <span class="hljs-comment">// 填写BaseEntity中的公共字段</span>
        strategy.setSuperEntityColumns(<span class="hljs-string">"id"</span>, <span class="hljs-string">"gmt_create"</span>, <span class="hljs-string">"gmt_modified"</span>);

        <span class="hljs-comment">// 6、执行</span>
        mpg.execute();
    &#125;
&#125;</code></pre>

<h3 id="优化代码生成器"><a href="#优化代码生成器" class="headerlink" title="优化代码生成器"></a>优化代码生成器</h3><blockquote>
<p>在真实情况下，每个表都会有三字段，ID，创建时间，更新时间</p>
<p>所以，这部分实体类可以抽取出来</p>
<p>一般我们在service_base中创建BaseEntity</p>
</blockquote>
<h4 id="首先创建一个基础实体类"><a href="#首先创建一个基础实体类" class="headerlink" title="首先创建一个基础实体类"></a>首先创建一个基础实体类</h4><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.jimu98.service.base.model;

<span class="hljs-meta">@Data</span>
<span class="hljs-meta">@EqualsAndHashCode</span>(callSuper = <span class="hljs-keyword">false</span>)
<span class="hljs-meta">@Accessors</span>(chain = <span class="hljs-keyword">true</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BaseEntity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span> </span>&#123;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID=<span class="hljs-number">1L</span>;

    <span class="hljs-meta">@ApiModelProperty</span>(value = <span class="hljs-string">"讲师ID"</span>)  <span class="hljs-comment">//这个是swagger的注解</span>
    <span class="hljs-meta">@TableId</span>(value = <span class="hljs-string">"id"</span>, type = IdType.ASSIGN_ID)
    <span class="hljs-keyword">private</span> String id;

    <span class="hljs-meta">@ApiModelProperty</span>(value = <span class="hljs-string">"创建时间"</span>)
    <span class="hljs-meta">@TableField</span>(fill = FieldFill.INSERT)
    <span class="hljs-keyword">private</span> Date gmtCreate;

    <span class="hljs-meta">@ApiModelProperty</span>(value = <span class="hljs-string">"更新时间"</span>)
    <span class="hljs-meta">@TableField</span>(fill = FieldFill.INSERT_UPDATE)
    <span class="hljs-keyword">private</span> Date gmtModified;

&#125;</code></pre>

<h4 id="然后修改代码生成器中内容"><a href="#然后修改代码生成器中内容" class="headerlink" title="然后修改代码生成器中内容"></a>然后修改代码生成器中内容</h4><pre><code class="hljs java"><span class="hljs-comment">//设置BaseEntity</span>
strategy.setSuperEntityClass(<span class="hljs-string">"com.atguigu.guli.service.base.model.BaseEntity"</span>);
<span class="hljs-comment">// 填写BaseEntity中的公共字段</span>
strategy.setSuperEntityColumns(<span class="hljs-string">"id"</span>, <span class="hljs-string">"gmt_create"</span>, <span class="hljs-string">"gmt_modified"</span>);</code></pre>

<h2 id="其他问题"><a href="#其他问题" class="headerlink" title="其他问题"></a>其他问题</h2><h3 id="注解"><a href="#注解" class="headerlink" title="注解"></a>注解</h3><blockquote>
<p>@TableId(type = IdType.ASSIGN_ID)</p>
</blockquote>
<ul>
<li>描述：主键注解</li>
</ul>
<table>
<thead>
<tr>
<th align="center">属性</th>
<th align="center">类型</th>
<th align="center">必须指定</th>
<th align="center">默认值</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">value</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">主键字段名</td>
</tr>
<tr>
<td align="center">type</td>
<td align="center">Enum</td>
<td align="center">否</td>
<td align="center">IdType.NONE</td>
<td align="center">主键类型</td>
</tr>
</tbody></table>
<ul>
<li>type</li>
</ul>
<table>
<thead>
<tr>
<th align="center">值</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">AUTO</td>
<td align="center">数据库ID自增</td>
</tr>
<tr>
<td align="center">NONE</td>
<td align="center">无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)</td>
</tr>
<tr>
<td align="center">INPUT</td>
<td align="center">insert前自行set主键值</td>
</tr>
<tr>
<td align="center">ASSIGN_ID</td>
<td align="center">分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口<code>IdentifierGenerator</code>的方法<code>nextId</code>(默认实现类为<code>DefaultIdentifierGenerator</code>雪花算法)</td>
</tr>
<tr>
<td align="center">ASSIGN_UUID</td>
<td align="center">分配UUID,主键类型为String(since 3.3.0),使用接口<code>IdentifierGenerator</code>的方法<code>nextUUID</code>(默认default方法)</td>
</tr>
<tr>
<td align="center">ID_WORKER</td>
<td align="center">分布式全局唯一ID 长整型类型(please use <code>ASSIGN_ID</code>)</td>
</tr>
<tr>
<td align="center">UUID</td>
<td align="center">32位UUID字符串(please use <code>ASSIGN_UUID</code>)</td>
</tr>
<tr>
<td align="center">ID_WORKER_STR</td>
<td align="center">分布式全局唯一ID 字符串类型(please use <code>ASSIGN_ID</code>)</td>
</tr>
</tbody></table>
<p>要想影响所有实体的配置，可以设置全局主键配置</p>
<pre><code class="hljs yaml"><span class="hljs-comment">#全局设置主键生成策略</span>
<span class="hljs-string">mybatis-plus.global-config.db-config.id-type=auto</span></code></pre>

<blockquote>
<p>@TableField  </p>
</blockquote>
<ul>
<li>描述：字段注解(非主键)</li>
</ul>
<table>
<thead>
<tr>
<th align="center">属性</th>
<th align="center">类型</th>
<th align="center">必须指定</th>
<th align="center">默认值</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">value</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">数据库字段名</td>
</tr>
<tr>
<td align="center">el</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">映射为原生 <code>#{ ... }</code> 逻辑,相当于写在 xml 里的 <code>#{ ... }</code> 部分</td>
</tr>
<tr>
<td align="center">exist</td>
<td align="center">boolean</td>
<td align="center">否</td>
<td align="center">true</td>
<td align="center">是否为数据库表字段</td>
</tr>
<tr>
<td align="center">condition</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">字段 <code>where</code> 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 <code>% s=#{ %s }</code>,<a href="https://github.com/baomidou/mybatis-plus/blob/3.0/mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/SqlCondition.java" target="_blank" rel="noopener">参考</a></td>
</tr>
<tr>
<td align="center">update</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">字段 <code>update set</code> 部分注入, 例如：update=”%s+1”：表示更新时会set version=version+1(该属性优先级高于 <code>el</code> 属性)</td>
</tr>
<tr>
<td align="center">insertStrategy</td>
<td align="center">Enum</td>
<td align="center">N</td>
<td align="center">DEFAULT</td>
<td align="center">举例：NOT_NULL: <code>insert into table_a(&lt;if test=&quot;columnProperty != null&quot;&gt;column&lt;/if&gt;) values (&lt;if test=&quot;columnProperty != null&quot;&gt;#{columnProperty}&lt;/if&gt;)</code></td>
</tr>
<tr>
<td align="center">updateStrategy</td>
<td align="center">Enum</td>
<td align="center">N</td>
<td align="center">DEFAULT</td>
<td align="center">举例：IGNORED: <code>update table_a set column=#{columnProperty}</code></td>
</tr>
<tr>
<td align="center">whereStrategy</td>
<td align="center">Enum</td>
<td align="center">N</td>
<td align="center">DEFAULT</td>
<td align="center">举例：NOT_EMPTY: <code>where &lt;if test=&quot;columnProperty != null and columnProperty!=&#39;&#39;&quot;&gt;column=#{columnProperty}&lt;/if&gt;</code></td>
</tr>
<tr>
<td align="center">fill</td>
<td align="center">Enum</td>
<td align="center">否</td>
<td align="center">FieldFill.DEFAULT</td>
<td align="center">字段自动填充策略</td>
</tr>
<tr>
<td align="center">select</td>
<td align="center">boolean</td>
<td align="center">否</td>
<td align="center">true</td>
<td align="center">是否进行 select 查询</td>
</tr>
<tr>
<td align="center">keepGlobalFormat</td>
<td align="center">boolean</td>
<td align="center">否</td>
<td align="center">false</td>
<td align="center">是否保持使用全局的 format 进行处理</td>
</tr>
<tr>
<td align="center">jdbcType</td>
<td align="center">JdbcType</td>
<td align="center">否</td>
<td align="center">JdbcType.UNDEFINED</td>
<td align="center">JDBC类型 (该默认值不代表会按照该值生效)</td>
</tr>
<tr>
<td align="center">typeHandler</td>
<td align="center">Class&lt;? extends TypeHandler&gt;</td>
<td align="center">否</td>
<td align="center">UnknownTypeHandler.class</td>
<td align="center">类型处理器 (该默认值不代表会按照该值生效)</td>
</tr>
<tr>
<td align="center">numericScale</td>
<td align="center">String</td>
<td align="center">否</td>
<td align="center">“”</td>
<td align="center">指定小数点后保留的位数</td>
</tr>
</tbody></table>
<blockquote>
<p>另外这个注解的包可能不会自动引入，有个小BUG，所以需要手动引入一下,其他的就看官方文档吧</p>
</blockquote>
<h3 id="自动填充"><a href="#自动填充" class="headerlink" title="自动填充"></a>自动填充</h3><blockquote>
<p>这个主要是为create_time、update_time进行自动填充</p>
</blockquote>
<h4 id="实体类修改"><a href="#实体类修改" class="headerlink" title="实体类修改"></a>实体类修改</h4><p>在实体类（如果前面提取了公共实体类，那么这里操作公共实体类）添加注解@TableField(fill = FieldFill.INSERT)</p>
<p>FieldFill是一个enum 枚举类型 </p>
<p>所以create_time设置INSERT  ，update_time设置 INSERT_UPDATE</p>
<h4 id="实现元对象处理器接口"><a href="#实现元对象处理器接口" class="headerlink" title="实现元对象处理器接口"></a>实现元对象处理器接口</h4><p><strong><code>注意：不要忘记添加 @Component 注解</code></strong></p>
<pre><code class="hljs java"><span class="hljs-meta">@Slf</span>4j
<span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyMetaObjectHandler</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MetaObjectHandler</span> </span>&#123;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertFill</span><span class="hljs-params">(MetaObject metaObject)</span> </span>&#123;
        log.info(<span class="hljs-string">"start insert fill ...."</span>);
        <span class="hljs-keyword">this</span>.setFieldValByName(<span class="hljs-string">"createTime"</span>, <span class="hljs-keyword">new</span> Date(), metaObject);
        <span class="hljs-keyword">this</span>.setFieldValByName(<span class="hljs-string">"updateTime"</span>, <span class="hljs-keyword">new</span> Date(), metaObject);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateFill</span><span class="hljs-params">(MetaObject metaObject)</span> </span>&#123;
        log.info(<span class="hljs-string">"start update fill ...."</span>);
        <span class="hljs-keyword">this</span>.setFieldValByName(<span class="hljs-string">"updateTime"</span>, <span class="hljs-keyword">new</span> Date(), metaObject);
    &#125;
&#125;</code></pre>



<h3 id="乐观锁"><a href="#乐观锁" class="headerlink" title="乐观锁"></a>乐观锁</h3><h4 id="修改实体类"><a href="#修改实体类" class="headerlink" title="修改实体类"></a>修改实体类</h4><pre><code class="hljs java"><span class="hljs-meta">@Version</span>
<span class="hljs-keyword">private</span> Integer version;</code></pre>

<h4 id="创建配置文件"><a href="#创建配置文件" class="headerlink" title="创建配置文件"></a>创建配置文件</h4><pre><code class="hljs java"><span class="hljs-meta">@EnableTransactionManagement</span>
<span class="hljs-meta">@Configuration</span>
<span class="hljs-meta">@MapperScan</span>(<span class="hljs-string">"cn.jimu98.xxx.mapper"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MybatisPlusConfig</span> </span>&#123;
    
&#125;</code></pre>

<h4 id="注册乐观锁插件"><a href="#注册乐观锁插件" class="headerlink" title="注册乐观锁插件"></a>注册乐观锁插件</h4><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment">     * 乐观锁插件</span>
<span class="hljs-comment">     */</span>
<span class="hljs-meta">@Bean</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> OptimisticLockerInterceptor <span class="hljs-title">optimisticLockerInterceptor</span><span class="hljs-params">()</span> </span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> OptimisticLockerInterceptor();
&#125;</code></pre>

<h3 id="分页"><a href="#分页" class="headerlink" title="分页"></a>分页</h3><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * 分页插件</span>
<span class="hljs-comment"> */</span>
<span class="hljs-meta">@Bean</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> PaginationInterceptor <span class="hljs-title">paginationInterceptor</span><span class="hljs-params">()</span> </span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> PaginationInterceptor();
&#125;</code></pre>



<h3 id="逻辑删除"><a href="#逻辑删除" class="headerlink" title="逻辑删除"></a>逻辑删除</h3><h4 id="数据库修改"><a href="#数据库修改" class="headerlink" title="数据库修改"></a>数据库修改</h4><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`user`</span> <span class="hljs-keyword">ADD</span> <span class="hljs-keyword">COLUMN</span> <span class="hljs-string">`deleted`</span> <span class="hljs-built_in">boolean</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">false</span></code></pre>

<h4 id="实体类修改-1"><a href="#实体类修改-1" class="headerlink" title="实体类修改"></a>实体类修改</h4><pre><code class="hljs java"><span class="hljs-meta">@TableLogic</span>
<span class="hljs-keyword">private</span> Integer deleted;</code></pre>

<h4 id="配置（可选）"><a href="#配置（可选）" class="headerlink" title="配置（可选）"></a>配置（可选）</h4><pre><code class="hljs yaml"><span class="hljs-string">mybatis-plus.global-config.db-config.logic-delete-value=1</span>
<span class="hljs-string">mybatis-plus.global-config.db-config.logic-not-delete-value=0</span></code></pre>



<h3 id="条件构造器"><a href="#条件构造器" class="headerlink" title="条件构造器"></a>条件构造器</h3><table>
<thead>
<tr>
<th><strong>查询方式</strong></th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td><strong>setSqlSelect</strong></td>
<td>设置 SELECT 查询字段</td>
</tr>
<tr>
<td><strong>where</strong></td>
<td>WHERE 语句，拼接 + WHERE 条件</td>
</tr>
<tr>
<td><strong>and</strong></td>
<td>AND 语句，拼接 + AND 字段=值</td>
</tr>
<tr>
<td><strong>andNew</strong></td>
<td>AND 语句，拼接 + AND (字段=值)</td>
</tr>
<tr>
<td><strong>or</strong></td>
<td>OR 语句，拼接 + OR 字段=值</td>
</tr>
<tr>
<td><strong>orNew</strong></td>
<td>OR 语句，拼接 + OR (字段=值)</td>
</tr>
<tr>
<td><strong>eq</strong></td>
<td>等于=</td>
</tr>
<tr>
<td><strong>allEq</strong></td>
<td>基于 map 内容等于=</td>
</tr>
<tr>
<td><strong>ne</strong></td>
<td>不等于&lt;&gt;</td>
</tr>
<tr>
<td><strong>gt</strong></td>
<td>大于&gt;</td>
</tr>
<tr>
<td><strong>ge</strong></td>
<td>大于等于&gt;=</td>
</tr>
<tr>
<td><strong>lt</strong></td>
<td>小于&lt;</td>
</tr>
<tr>
<td><strong>le</strong></td>
<td>小于等于&lt;=</td>
</tr>
<tr>
<td><strong>like</strong></td>
<td>模糊查询 LIKE</td>
</tr>
<tr>
<td><strong>notLike</strong></td>
<td>模糊查询 NOT LIKE</td>
</tr>
<tr>
<td><strong>in</strong></td>
<td>IN 查询</td>
</tr>
<tr>
<td><strong>notIn</strong></td>
<td>NOT IN 查询</td>
</tr>
<tr>
<td><strong>isNull</strong></td>
<td>NULL 值查询</td>
</tr>
<tr>
<td><strong>isNotNull</strong></td>
<td>IS NOT NULL</td>
</tr>
<tr>
<td><strong>groupBy</strong></td>
<td>分组 GROUP BY</td>
</tr>
<tr>
<td><strong>having</strong></td>
<td>HAVING 关键词</td>
</tr>
<tr>
<td><strong>orderBy</strong></td>
<td>排序 ORDER BY</td>
</tr>
<tr>
<td><strong>orderAsc</strong></td>
<td>ASC 排序 ORDER BY</td>
</tr>
<tr>
<td><strong>orderDesc</strong></td>
<td>DESC 排序 ORDER BY</td>
</tr>
<tr>
<td><strong>exists</strong></td>
<td>EXISTS 条件语句</td>
</tr>
<tr>
<td><strong>notExists</strong></td>
<td>NOT EXISTS 条件语句</td>
</tr>
<tr>
<td><strong>between</strong></td>
<td>BETWEEN 条件语句</td>
</tr>
<tr>
<td><strong>notBetween</strong></td>
<td>NOT BETWEEN 条件语句</td>
</tr>
<tr>
<td><strong>addFilter</strong></td>
<td>自由拼接 SQL</td>
</tr>
<tr>
<td><strong>last</strong></td>
<td>拼接在最后，例如：last(“LIMIT 1”)</td>
</tr>
</tbody></table>
<p>其他的就看官网吧<a href="https://mp.baomidou.com/guide/wrapper.html#abstractwrapper" target="_blank" rel="noopener">https://mp.baomidou.com/guide/wrapper.html#abstractwrapper</a></p>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA/">项目构建</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/SpringCloud/">SpringCloud</a>
                    
                      <a class="hover-with-bg" href="/tags/%E6%A1%86%E6%9E%B6/">框架</a>
                    
                      <a class="hover-with-bg" href="/tags/MyBatis-Plus/">MyBatis-Plus</a>
                    
                      <a class="hover-with-bg" href="/tags/%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%99%A8/">代码生成器</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2020/07/08/goujian/4.%E6%99%AE%E9%80%9A%E6%8E%A5%E5%8F%A3%E5%BC%80%E5%8F%91/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【项目创建流程】普通接口开发</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2020/07/08/goujian/2.%E9%A1%B9%E7%9B%AE%E6%A1%86%E6%9E%B6%E6%9E%84%E5%BB%BA/">
                        <span class="hidden-mobile">【项目创建流程】项目框架构建</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    function loadValine() {
      addScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "NFojgagn3zsFUjT0FGVVHUTM-gzGzoHsz",
          app_key: "9R1L32c7WEvSGSP48NAnfStY",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: true,
          serverURLs: "https://nfojgagn.lc-cn-n1-shared.com",
        });
      });
    }
    createObserver(loadValine, 'vcomments');
  </script>
  <noscript>Please enable JavaScript to view the <a href="https://valine.js.org" target="_blank" rel="nofollow noopener noopener">comments
      powered by Valine.</a></noscript>


              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
  <p id="hitokoto">:D 获取中...</p>
<script>
  fetch('https://v1.hitokoto.cn')
    .then(response => response.json())
    .then(data => {
      const hitokoto = document.getElementById('hitokoto')
      hitokoto.innerText = data.hitokoto
      })
      .catch(console.error)
</script>
   <div>
  <span id="timeDate">载入天数...</span>
  <span id="times">载入时分秒...</span>
  <script>
  var now = new Date();
  function createtime(){
      var grt= new Date("07/18/2020 18:00:00");//此处修改你的建站时间或者网站上线时间
      now.setTime(now.getTime()+250);
      days = (now - grt ) / 1000 / 60 / 60 / 24;
      dnum = Math.floor(days);
      hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum);
      hnum = Math.floor(hours);
      if(String(hnum).length ==1 ){
          hnum = "0" + hnum;
      }
      minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
      mnum = Math.floor(minutes);
      if(String(mnum).length ==1 ){
                mnum = "0" + mnum;
      }
      seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
      snum = Math.round(seconds);
      if(String(snum).length ==1 ){
                snum = "0" + snum;
      }
      document.getElementById("timeDate").innerHTML = "本站安全运行&nbsp"+dnum+"&nbsp天";
      document.getElementById("times").innerHTML = hnum + "&nbsp小时&nbsp" + mnum + "&nbsp分&nbsp" + snum + "&nbsp秒";
  }
  setInterval("createtime()",250);
  </script>
</div>
    <div>
      &copy;2019 - 2020 By Jimu98
    </div>
    

    
  <!-- 备案信息 -->
  <div class="beian">
		
          <img src="/img/police_beian.png" srcset="/img/loading.gif" alt="police-icon" />
        
    <a href="http://beian.miit.gov.cn/" target="_blank"
       rel="nofollow noopener">冀ICP备19035627号</a>
    
  </div>


    
      <!-- cnzz Analytics Icon -->
      <span id="cnzz_stat_icon_1278280985" style="display: none"></span>
    
  </div>

</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>







  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "【项目创建流程】MyBatisPlas流程&nbsp;",
      ],
      cursorChar: "|",
      typeSpeed: 95,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  











  

  

  

  

  

  
    <!-- cnzz Analytics -->
    <script defer src="//s4.cnzz.com/z_stat.php?id=1278280985&show=pic"
            type="text/javascript"></script>
  





</body>
</html>
